// 用户登录
document.querySelector('#btn-login').addEventListener('click', async () => {
    const form = document.querySelector('.login-form')
    const data = serialize(form, { hash: true, empty: true })
    const { username, password } = data
    if (username === '' || password === '') {
        showToast('用户名或密码不能为空')
        return
    }
    if (username.length < 8 || username.length > 30 || password.length < 6 || password.length > 30) {
        showToast('用户名长度8-30，密码长度6-30')
        return
    }
    // 提交数据
    try {
        const res = await axios.post('/login', { username, password })
        showToast(res.message)
        localStorage.setItem('token', res.data.token)
        localStorage.setItem('username', res.data.username)
        setTimeout(() => {
            location.href = './index.html'
        }, 1500)
    } catch (err) {
        showToast(err.response.data.message)
    }
})